function doSomething1(arg: any): any {
  if (Array.isArray(arg)) {
    return arg.length;
  }
  return arg;
}

// const foo = doSomething1([1, 23]) as number;
const foo = doSomething1({}) as number;
// foo.toFixed(); // foo是有number的方法，但是不可以用，因为你返回的是object，
// 所以在使用as type的时候是需要非常小心的，你一定要确认它是返回你要的类型
// foo这里显示any类型，当你使用类型断言的时候可以强制他返回你要的类型

// 但是当你传入的值是object的时候，他就抱错了

// challenge Trim Strings

// 递归实现trim函数
function trimStrings<T>(value: T): T {
  switch (typeof value) {
    case "string":
      return value.trim() as T;
    case "object":
      if (value === null) return value;
      // TODO: if value is an array, map over it and trim eacg value
      if (Array.isArray(value)) {
        return value.map(trimStrings) as T;
      }
      // TODO: if value si an object, map over it and trim each value
      for (const key in value) {
        value[key] = trimStrings(value[key]);
      }
      return value;

    default:
      return value;
  }
}

const x = "hello";
const res1 = trimStrings(x);
console.log(res1);

const testObje = {
  name: "  Johna Doe",
  age: 31,
  address: {
    street: " 21321 Main St",
    city: " Aynytom",
    state: "  Ca",
  },
  hobbies: ["  COding", "  readings", "  gamins  ", [" axx ", [" w "]]],
};

const trimObj = trimStrings(testObje);
console.log(trimObj);
